package com.woniuxy.microaftersaleserver.consultmag.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.microaftersaleserver.consultmag.service.IConsultService;
import com.woniuxy.microaftersaleserver.consultmag.service.impl.ConsultServiceImpl;
import com.woniuxy.microcommonserver.bean.aftersales_cyx.ConsultGoods;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microcommonserver.pageHelper.TableResult;
import com.woniuxy.microcommonserver.res.ResultMsg;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RequestMapping("/consult")
@RestController
public class ConsultController {

    @Resource
    private IConsultService consultServiceImpl;

    @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg addConsultService(@RequestBody ConsultGoods consultGoods){
        System.out.println(consultGoods);
        consultServiceImpl.addConsultRecord(consultGoods);
        return new ResultMsg(200,"添加成功",null);
    }

    @PutMapping(value = "/deleted",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg deletedConsultService(int id){
        consultServiceImpl.deleteConsultRecord(id);
        System.out.println();
        return new ResultMsg(200,"删除成功",null);
    }

    @PutMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg updateConsultService(@RequestBody ConsultGoods consultGoods){
        consultServiceImpl.updateConsultRecord(consultGoods);
        return new ResultMsg(200,"修改成功",null);
    }

    /**
     * 根据字段查询咨询信息
     * @param consultGoods
     * @return
     */
    @GetMapping(value = "/get",produces = MediaType.APPLICATION_JSON_VALUE)
    public TableResult findByParams(ConsultGoods consultGoods){
       List<Map> records= consultServiceImpl.findByParams(consultGoods);
        System.out.println(records);
        return new TableResult(null,records);
    }

    /**
     * 分页查询所有咨询记录
     * @param request
     * @param consultGoods
     * @return
     */
    @GetMapping(value = "/all",produces = MediaType.APPLICATION_JSON_VALUE)
    public TableResult<ConsultGoods> findAll(PageRequest request, ConsultGoods consultGoods){
        System.out.println(request);
        System.out.println(consultGoods);
        PageInfo<ConsultGoods> pageInfo=consultServiceImpl.findAll(request,consultGoods);
        System.out.println(pageInfo);
        return  new TableResult<>(pageInfo.getTotal(),pageInfo.getList());
    }
}
